﻿using BaseDll;
using CommonTools;
using Sunny.UI;
using System;
namespace StationDemo.FrameUI
{
    public partial class Form_RunSetting : UIPage
    {
        public Form_RunSetting()
        {
            InitializeComponent();
        }
        public UserRight userRight
        {
            get;
            set;
        }
        private void RunSettingForm_Initialize(object sender, EventArgs e)
        {
            userRight = UserRight.User_None;
            sys.g_eventRightChanged += ChangedUserRight;
            ChangedUserRight(sys.g_User);
            checkBox_ModeAirRun.Checked = sys.g_AppMode == AppMode.AirRun;
            check_CloseSafeProtect.Checked = false;
        }

        public void ChangedUserRight(User currentUser)
        {

            if (userRight == currentUser._userRight)
                return;
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => ChangedUserRight(currentUser)));
            }
            else
            {
                userRight = currentUser._userRight;

                if ((int)currentUser._userRight < (int)UserRight.Software)
                {
                    for (int i = 0; i < panSafeSet.Controls.Count; i++)
                        panSafeSet.Controls[i].Enabled = false;
                    for (int i = 0; i < panRunSet.Controls.Count; i++)
                        panRunSet.Controls[i].Enabled = false;
                }
                else
                {
                    for (int i = 0; i < panSafeSet.Controls.Count; i++)
                        panSafeSet.Controls[i].Enabled = true;
                    for (int i = 0; i < panRunSet.Controls.Count; i++)
                        panRunSet.Controls[i].Enabled = true;
                }

            }
        }


        private void check_CloseSafeProtect_ValueChanged(object sender, bool value)
        {
            GlobalVariable.启用安全保护 = !check_CloseSafeProtect.Checked;
            if (GlobalVariable.启用安全保护)
            {
                Safe.AddAllSafeCheck();
            }
            else
            {
                Safe.DelectAllSafeCheck();
            }

        }

        private void checkBox_ModeAirRun_ValueChanged(object sender, bool value)
        {
            if (checkBox_ModeAirRun.Checked)
                checkBox_ModeRun.Checked = false;
            sys.g_AppMode = checkBox_ModeAirRun.Checked ? AppMode.AirRun : AppMode.Run;
        }

        private void checkBox_ModeRun_ValueChanged(object sender, bool value)
        {
            if (checkBox_ModeRun.Checked)
                checkBox_ModeAirRun.Checked = false;
            sys.g_AppMode = checkBox_ModeAirRun.Checked ? AppMode.AirRun : AppMode.Run;
        }
    }
}
